package com.ruoyi.entity.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.entity.domain.Option;
import com.ruoyi.entity.service.IOptionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 配置Controller
 *
 * @author Delamer
 * @date 2020-12-23
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/entity/option")
public class OptionController extends BaseController {

    private final IOptionService optionService;

    /**
     * 查询配置列表
     */
    @PreAuthorize("@ss.hasPermi('entity:option:list')")
    @GetMapping("/list")
    public TableDataInfo list(Option option) {
        startPage();
        LambdaQueryWrapper<Option> lqw = new LambdaQueryWrapper<Option>();
        if (StringUtils.isNotBlank(option.getOptionKey())) {
            lqw.eq(Option::getOptionKey, option.getOptionKey());
        }
        if (StringUtils.isNotBlank(option.getOptionValue())) {
            lqw.eq(Option::getOptionValue, option.getOptionValue());
        }
        if (StringUtils.isNotBlank(option.getOptionIntro())) {
            lqw.eq(Option::getOptionIntro, option.getOptionIntro());
        }
        if (option.getSort() != null) {
            lqw.eq(Option::getSort, option.getSort());
        }
        lqw.orderByAsc(Option::getSort);
        List<Option> list = optionService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出配置列表
     */
    @PreAuthorize("@ss.hasPermi('entity:option:export')")
    @Log(title = "配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Option option) {
        LambdaQueryWrapper<Option> lqw = new LambdaQueryWrapper<Option>(option);
        List<Option> list = optionService.list(lqw);
        ExcelUtil<Option> util = new ExcelUtil<Option>(Option.class);
        return util.exportExcel(list, "option");
    }

    /**
     * 获取配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('entity:option:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(optionService.getById(id));
    }

    /**
     * 新增配置
     */
    @PreAuthorize("@ss.hasPermi('entity:option:add')")
    @Log(title = "配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Option option) {
        return toAjax(optionService.save(option) ? 1 : 0);
    }

    /**
     * 修改配置
     */
    @PreAuthorize("@ss.hasPermi('entity:option:edit')")
    @Log(title = "配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Option option) {
        return toAjax(optionService.updateById(option) ? 1 : 0);
    }

    /**
     * 删除配置
     */
    @PreAuthorize("@ss.hasPermi('entity:option:remove')")
    @Log(title = "配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(optionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
